<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;
class LinkController extends Controller
{
    //打开数据添加页面
    public function getAdd()
    {
        return view('admin.link.add');
    }
    //数据插入
    public function postInsert(Request $request)
    {
        
        //获取提交值
        $data=$request->except(['_token']);
        // dd($data);
        //数据写入数据库
        $res=DB::table('link')->insert($data);
        if($res){
            return redirect('link/list')->with('info','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }

    //列表显示
    public function getList(Request $request)
    {
        $links=DB::table('link')->
            orderBy('id','desc')->
            where(function($query)use($request){
                $keyword=$request->input('keyword');
                if($keyword){
                    $query->where('name','like','%'.$keyword.'%');
                }
            })
            ->paginate($request->input('num',10));
        return view('admin.link.list',['links'=>$links,'request'=>$request]);
    }
    //跳转到更改数据页面
    public function getEdit(Request $request)
    {
        $id=$request->input('id');
        $link=DB::table('link')->where('id',$id)->first();
        // dd($link);
        return view('admin.link.edit',['link'=>$link]);
    }
    //信息修改
    public function postUpdate(Request $request)
    {
        //获取提交值
        $data=$request->except(['_token','id']);
        //数据写入数据库
        $res=DB::table('link')->where('id',$request->input('id'))->update($data);
        if($res){
            return redirect('link/list')->with('info','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }
    //信息删除
    public function getDrop(Request $request)
    {
        $id=$request->input('id');
        $res=DB::table('link')->where('id',$id)->delete();
        if($res){
            return redirect('link/list')->with('info','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    // public function getDetail(Request $request)
    // {
    //     $id=$request->input('id');
    //     // dd($id);
    //     $linkdetail=DB::table('link') 
    //     ->join('linkdetail','link.id','=','linkdetail.link_id')
    //     ->where('link.id','=',$id)
    //     ->first();
    //     return view('admin.link.detail',['detail',$linkdetail]);
    // }
}
